var OPTIONS={
    host : "http://localhost:8080/KiWi2/"
}

var link={

    loaded : false,
    href : "",
    document : undefined,
    helper : {
        getWindow : function() {
            var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                    .getInterface(Components.interfaces.nsIWebNavigation)
                                    .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                                    .rootTreeItem.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                    .getInterface(Components.interfaces.nsIDOMWindow);
            try {
                var safeWin = mainWindow.content;
                var unsafeWin = safeWin.wrappedJSObject;
                if (!("XPCSafeJSObjectWrapper" in this)) {
                    var wrappedWin = XPCNativeWrapper.unwrap(unsafeWin);
                    return wrappedWin;
                } else {
                    var wrappedWin = new XPCSafeJSObjectWrapper(unsafeWin);
                    return wrappedWin;
                }
            }
            catch (err) {
                Components.utils.reportError(err);
                return undefined;
            }
        }
    },

    start : function() {
        //test if script already loaded
        if(window.content.window.location != link.href) {
            link.href = window.content.window.location;
            link.loaded = false;
        }
        if(link.loaded) {
            link.document.getElementById("link_ff_extension_button").click();
            return;
        }
        link.loaded = true;

        //get prefs
        var pref_manager = Components.classes["@mozilla.org/preferences-service;1"]
         .getService(Components.interfaces.nsIPrefService)
         .getBranch("extensions.link.");

        if(!pref_manager.prefHasUserValue("url") || pref_manager.getCharPref("url") == "") {
            window.alert("Host is set to default and can be changed in extension options");
            pref_manager.setCharPref("url",OPTIONS.host);
        }
        var script_src = pref_manager.getCharPref("url")+"sources/link_ff_extension_files/js/link_ff_extension_main.js";

        //load script
        var w = link.helper.getWindow();
        link.document = w.document;
        var div = link.document.createElement('div');
        div.setAttribute('id', "link_ff_extension_div");

        var script = link.document.createElement('script');
        script.setAttribute('src', script_src);
        div.appendChild(script);

        link.document.documentElement.appendChild(div);

        //add handler
        div.addEventListener("my_event", handler.onSelectionDone, false);
    }
}

var handler={

    onSelectionDone : function(event) {
        //get data
        var sel = link.document.getElementById('link_ff_extension_selected').childNodes;
        var obj = "";
        for(var i=0; i<sel.length;i++) {
            var x = sel[i].childNodes[0].innerHTML;
            x = x.substring(4,x.length-3);
            obj = obj + x;
        }
        var iframes = window.content.document.getElementsByTagName('iframe');
        for (var i = 0; i < iframes.length; i++) {
            var iframe = iframes[i];
            if(iframe.name == "link_ff_extension_iframe1") {
                iframe.contentDocument.getElementById("selection").innerHTML = "<!--"+obj+"-->";
                iframe.parentNode.style.display = "block";
                //signal to open widgets
                iframe.contentDocument.getElementById("start").innerHTML = "true";
            }
        }

    }

}